// ignore_for_file: non_constant_identifier_names

import 'dart:math';

import 'package:tiktokadult/hj_model/home/module_detail_model.dart';
import 'package:tiktokadult/hj_model/message/search_user_obj.dart';
import 'package:tiktokadult/hj_model/splash/ads_model.dart';
import 'package:tiktokadult/hj_model/video_model.dart';

class AdSubResult {
  final List<AdsInfoModel> ads;
  final int index;

  AdSubResult({required this.ads, required this.index});
}

class AdInsertManager {
  static AdSubResult _subList(List<AdsInfoModel> list, int adIndex, int? adCount) {
    if (adCount == null) {
      return AdSubResult(ads: list, index: adIndex);
    }
    if (adIndex >= list.length) {
      adIndex = 0;
    }
    // 一次取adCount个，如果当前index+adCount大于list长度，取到数组末尾之后再从0开始取surplus个
    int surplus = adIndex + adCount - list.length;
    int end = min(adIndex + adCount, list.length);
    final List<AdsInfoModel> adsToInsert = list.sublist(adIndex, end);
    adIndex = end;
    if (surplus > 0) {
      final result = _subList(list, 0, surplus);
      adsToInsert.addAll(result.ads);
      adIndex = result.index;
      surplus = 0;
    }
    return AdSubResult(ads: adsToInsert, index: adIndex);
  }

  static void section(List<AllSection> modelArr, List<AdsInfoModel> advList, {int adGap = 5, int? adCount}) {
    if (advList.isEmpty || modelArr.isEmpty) {
      return;
    }
    int countGap = 0;
    List<AdsInfoModel> randomAdArr = advList;
    modelArr.removeWhere((it) => it.isAdsArr());
    for (int i = 0; i < modelArr.length; i++) {
      AllSection model = modelArr[i];
      if (countGap >= adGap) {
        countGap = 0;
        if (model.isAdsArr()) {
        } else {
          AllSection adModel = AllSection();
          adModel.adsInfoArr = randomAdArr;
          modelArr.insert(i, adModel);
        }
      } else {
        if (model.isAdsArr()) {
          countGap = 0;
        } else {
          countGap++;
        }
      }
      if (countGap == adGap && i == modelArr.length - 1) {
        AllSection adModel = AllSection();
        adModel.adsInfoArr = randomAdArr;
        modelArr.add(adModel);
        break;
      }
    }
  }

  //社区帖子: 最新中固定每个6个帖子出现一行热门UP主推荐展示两次后结束
  //展示位置第一排前12个，第二排13-24
  static void postUpUser(List<VideoModel> modelArr, List<HotBloggerEntity> hotUpList) {
    if (hotUpList.isEmpty) return;
    modelArr.removeWhere((element) => element.isHotUp());
    if (modelArr.length >= 6) {
      VideoModel fModel = VideoModel();
      if (hotUpList.length < 12) {
        fModel.hotUpList = hotUpList;
      } else {
        fModel.hotUpList = hotUpList.sublist(0, 12);
      }
      modelArr.insert(6, fModel);
      if (modelArr.length > 12) {
        VideoModel sModel = VideoModel();
        if (hotUpList.length > 12) {
          sModel.hotUpList = hotUpList.sublist(12, hotUpList.length);
        } else {
          sModel.hotUpList = hotUpList;
        }
        modelArr.insert(13, sModel);
      }
    }
  }

  static void video(List<VideoModel> modelArr, List<AdsInfoModel> advList, {int adGap = 5}) {
    if (advList.isEmpty || modelArr.isEmpty) {
      return;
    }
    int countGap = 0;
    int adIndex = 0;
    List<AdsInfoModel> randomAdArr = advList;
    modelArr.removeWhere((it) => it.isAdsArr());
    for (int i = 0; i < modelArr.length; i++) {
      VideoModel model = modelArr[i];
      if (countGap >= adGap) {
        countGap = 0;
        if (model.isRandomAd()) {
        } else {
          VideoModel adModel = VideoModel();
          adModel.randomAdsInfo = advList[adIndex % (randomAdArr.length)];
          modelArr.insert(i, adModel);
          adIndex++;
        }
      } else {
        if (model.isAdsArr()) {
          countGap = 0;
        } else {
          countGap++;
        }
      }
      if (countGap == adGap && i == modelArr.length - 1) {
        VideoModel adModel = VideoModel();
        adModel.randomAdsInfo = advList[adIndex % (randomAdArr.length)];
        modelArr.add(adModel);
        adIndex++;
        break;
      }
    }
  }
}
